﻿using DevExpress.Charts.Native;
using DevExpress.Utils;
using DevExpress.Xpo.DB.Helpers;
using DevExpress.XtraCharts.Design;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraPrinting.HtmlExport.Native;
//using DevExpress.XtraReports.Interactivity;
using DevExpress.XtraTreeList;
using Shopfloormanagement;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.ImageList;


namespace Shopfloormanagement
{
    public partial class Step8 : DevExpress.XtraEditors.XtraForm
    {
        public Step8()
        {
            InitializeComponent();
        }
        int step1;
        int step2;
        int step3;
        int step4;
        int step5;
        int step6;
        int step7;
        int step8;
        int step9;
        private void Word()//加载进度下方文字
        {
            lblStep1.Text = "Problem\nStatement";
            lblStep2.Text = "Problem\nDescription";
            lblStep3.Text = "Interim\nContainment\nactions";
            lblStep4.Text = "Identify\nPossible\nCauses";
            lblStep5.Text = "Test all\nPossible\nCauses";
            lblStep6.Text = "Verify\nPotential\nRoot Cause";
            lblStep7.Text = "Confirmed\nRoot Cause";
            lblStep8.Text = "Permanent\nCorrective\nActions";
            lblStep9.Text = "Further\nActivities";

        }
        private void LoadA3list()//获取上方图片状态
        {

            DataTable A3list = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_List", $"ID = '{rootcause.A3number}'");

            step1 = (int)A3list.Rows[0][6];
            step2 = (int)A3list.Rows[0][7];
            step3 = (int)A3list.Rows[0][8];
            step4 = (int)A3list.Rows[0][9];
            step5 = (int)A3list.Rows[0][10];
            step6 = (int)A3list.Rows[0][11];
            step7 = (int)A3list.Rows[0][12];
            step8 = (int)A3list.Rows[0][13];
            step9 = (int)A3list.Rows[0][14];
            LoadPicture(step1, step2, step3, step4, step5, step6, step7, step8, step9);
        }
        private void LoadPicture(int condition1, int condition2, int condition3, int condition4, int condition5, int condition6, int condition7, int condition8, int condition9)
        {
            switch (condition1)
            {
                case 1:
                    picstep1.Image = Properties.Resources.G1;
                    break;
                case 2:
                    picstep1.Image = Properties.Resources.B1;
                    break;
                case 3:
                    picstep1.Image = Properties.Resources.R1;
                    break;
                case 4:
                    picstep1.Image = Properties.Resources.H1;
                    break;
                default:
                    picstep1.Image = null;
                    break;
            }
            switch (condition2)
            {
                case 1:
                    picstep2.Image = Properties.Resources.G2;
                    break;
                case 2:
                    picstep2.Image = Properties.Resources.B2;
                    break;
                case 3:
                    picstep2.Image = Properties.Resources.R2;
                    break;
                case 4:
                    picstep2.Image = Properties.Resources.H2;
                    break;
                default:
                    picstep2.Image = null;
                    break;
            }
            switch (condition3)
            {
                case 1:
                    picstep3.Image = Properties.Resources.G3;
                    break;
                case 2:
                    picstep3.Image = Properties.Resources.B3;
                    break;
                case 3:
                    picstep3.Image = Properties.Resources.R3;
                    break;
                case 4:
                    picstep3.Image = Properties.Resources.H3;
                    break;
                default:
                    picstep3.Image = null;
                    break;
            }
            switch (condition4)
            {
                case 1:
                    picstep4.Image = Properties.Resources.G4;
                    break;
                case 2:
                    picstep4.Image = Properties.Resources.B4;
                    break;
                case 3:
                    picstep4.Image = Properties.Resources.R4;
                    break;
                case 4:
                    picstep4.Image = Properties.Resources.H4;
                    break;
                default:
                    picstep4.Image = null;
                    break;
            }
            switch (condition5)
            {
                case 1:
                    picstep5.Image = Properties.Resources.G5;
                    break;
                case 2:
                    picstep5.Image = Properties.Resources.B5;
                    break;
                case 3:
                    picstep5.Image = Properties.Resources.R5;
                    break;
                case 4:
                    picstep5.Image = Properties.Resources.H5;
                    break;
                default:
                    picstep5.Image = null;
                    break;
            }
            switch (condition6)
            {
                case 1:
                    picstep6.Image = Properties.Resources.G6;
                    break;
                case 2:
                    picstep6.Image = Properties.Resources.B6;
                    break;
                case 3:
                    picstep6.Image = Properties.Resources.R6;
                    break;
                case 4:
                    picstep6.Image = Properties.Resources.H6;
                    break;
                default:
                    picstep6.Image = null;
                    break;
            }
            switch (condition7)
            {
                case 1:
                    picstep7.Image = Properties.Resources.G7;
                    break;
                case 2:
                    picstep7.Image = Properties.Resources.B7;
                    break;
                case 3:
                    picstep7.Image = Properties.Resources.R7;
                    break;
                case 4:
                    picstep7.Image = Properties.Resources.H7;
                    break;
                default:
                    picstep7.Image = null;
                    break;
            }
            switch (condition8)
            {
                case 1:
                    picstep8.Image = Properties.Resources.G8;
                    break;
                case 2:
                    picstep8.Image = Properties.Resources.B8;
                    break;
                case 3:
                    picstep8.Image = Properties.Resources.R8;
                    break;
                case 4:
                    picstep8.Image = Properties.Resources.H8;
                    break;
                default:
                    picstep8.Image = null;
                    break;
            }
            switch (condition9)
            {
                case 1:
                    picstep9.Image = Properties.Resources.G9;
                    break;
                case 2:
                    picstep9.Image = Properties.Resources.B9;
                    break;
                case 3:
                    picstep9.Image = Properties.Resources.R9;
                    break;
                case 4:
                    picstep9.Image = Properties.Resources.H9;
                    break;
                default:
                    picstep9.Image = null;
                    break;
            }

        }
        private void LoadA3step8()//加载信息及判断是否过期
        {
            //获取选中A3的数据
            DataTable A3Step8 = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step8", $"A3ReportNumberID = '{rootcause.A3number}'");
            if (A3Step8.Rows.Count == 0) return; // 如果没有数据，直接返回

            DataRow row = A3Step8.Rows[0]; // 取第 0 行数据

            //判断有几个Action,显示对应数量的控件
            string action4 = A3Step8.Rows[0][5].ToString();
            string action5 = A3Step8.Rows[0][6].ToString();
            string action6 = A3Step8.Rows[0][7].ToString();
            string action7 = A3Step8.Rows[0][8].ToString();
            string action8 = A3Step8.Rows[0][9].ToString();
            string action9 = A3Step8.Rows[0][10].ToString();
            if (action9 != "")
            {
                lbl4.Visible = false;
                lbl5.Visible = false;
                lbl6.Visible = false;
                lbl7.Visible = false;
                lbl8.Visible = false;
                lbl9.Visible = false;
                pal4.Visible = true;
                pal5.Visible = true;
                pal6.Visible = true;
                pal7.Visible = true;
                pal8.Visible = true;
                pal9.Visible = true;
            }
            if (action9 == "" && action8 != "")
            {
                lbl4.Visible = false;
                lbl5.Visible = false;
                lbl6.Visible = false;
                lbl7.Visible = false;
                lbl8.Visible = false;
                lbl9.Visible = true;
                pal4.Visible = true;
                pal5.Visible = true;
                pal6.Visible = true;
                pal7.Visible = true;
                pal8.Visible = true;
                pal9.Visible = false;
            }
            if (action9 == "" && action8 == "" && action7 != "")
            {
                lbl4.Visible = false;
                lbl5.Visible = false;
                lbl6.Visible = false;
                lbl7.Visible = false;
                lbl8.Visible = true;
                lbl9.Visible = false;
                pal4.Visible = true;
                pal5.Visible = true;
                pal6.Visible = true;
                pal7.Visible = true;
                pal8.Visible = false;
                pal9.Visible = false;
            }
            if (action9 == "" && action8 == "" && action7 == "" && action6 != "")
            {
                lbl4.Visible = false;
                lbl5.Visible = false;
                lbl6.Visible = false;
                lbl7.Visible = true;
                lbl8.Visible = false;
                lbl9.Visible = false;
                pal4.Visible = true;
                pal5.Visible = true;
                pal6.Visible = true;
                pal7.Visible = false;
                pal8.Visible = false;
                pal9.Visible = false;
            }
            if (action9 == "" && action8 == "" && action7 == "" && action6 == "" && action5 != "")
            {
                lbl4.Visible = false;
                lbl5.Visible = false;
                lbl6.Visible = true;
                lbl7.Visible = false;
                lbl8.Visible = false;
                lbl9.Visible = false;
                pal4.Visible = true;
                pal5.Visible = true;
                pal6.Visible = false;
                pal7.Visible = false;
                pal8.Visible = false;
                pal9.Visible = false;
            }
            if (action9 == "" && action8 == "" && action7 == "" && action6 == "" && action5 == "" && action4 != "")
            {
                lbl4.Visible = false;
                lbl5.Visible = true;
                lbl6.Visible = false;
                lbl7.Visible = false;
                lbl8.Visible = false;
                lbl9.Visible = false;
                pal4.Visible = true;
                pal5.Visible = false;
                pal6.Visible = false;
                pal7.Visible = false;
                pal8.Visible = false;
                pal9.Visible = false;
            }
            if (action9 == "" && action8 == "" && action7 == "" && action6 == "" && action5 == "" && action4 == "")
            {
                lbl4.Visible = true;
                lbl5.Visible = false;
                lbl6.Visible = false;
                lbl7.Visible = false;
                lbl8.Visible = false;
                lbl9.Visible = false;
                pal4.Visible = false;
                pal5.Visible = false;
                pal6.Visible = false;
                pal7.Visible = false;
                pal8.Visible = false;
                pal9.Visible = false;
            }
            // 加载Action
            for (int i = 1; i <= 9; i++)
            {
                string columnName = $"Action{i}";

                // 确保列存在
                if (!A3Step8.Columns.Contains(columnName)) continue;

                // 获取 TextEdit 控件
                Control[] controls = this.Controls.Find($"txtaction{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.TextEdit textEdit)
                {
                    // 赋值，忽略 NULL
                    textEdit.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
            // 加载Who
            for (int i = 1; i <= 9; i++)
            {
                string columnName = $"Who{i}";

                // 确保列存在
                if (!A3Step8.Columns.Contains(columnName)) continue;

                // 获取 TextEdit 控件
                Control[] controls = this.Controls.Find($"txtwho{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.TextEdit textEdit)
                {
                    // 赋值，忽略 NULL
                    textEdit.Text = row[columnName] != DBNull.Value ? row[columnName].ToString() : string.Empty;
                }
            }
            // 加载When
            for (int i = 1; i <= 9; i++)
            {
                string columnName = $"When{i}";

                // 确保列存在
                if (!A3Step8.Columns.Contains(columnName)) continue;

                // 获取 DateEdit 控件
                Control[] controls = this.Controls.Find($"dtwhen{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.DateEdit dateEdit)
                {
                    // 赋值，忽略 NULL
                    if (row[columnName] != DBNull.Value)
                    {
                        dateEdit.DateTime = Convert.ToDateTime(row[columnName]);
                    }
                    else
                    {
                        dateEdit.EditValue = null; // 清空 DateEdit，避免显示默认日期
                    }
                }
            }
            // 加载Status
            for (int i = 1; i <= 9; i++)
            {
                string columnName = $"Status{i}";  // 列名：Int1, Int2, Int3...

                // 确保列存在
                if (!A3Step8.Columns.Contains(columnName)) continue;

                // 获取 ComboBoxEdit 控件
                Control[] controls = this.Controls.Find($"cobstatus{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.ComboBoxEdit comboBoxEdit)
                {
                    // 赋值，忽略 NULL
                    if (row[columnName] != DBNull.Value)
                    {
                        comboBoxEdit.EditValue = Convert.ToInt32(row[columnName]);
                    }
                    else
                    {
                        comboBoxEdit.EditValue = null; // 清空 ComboBoxEdit，避免显示错误值
                    }
                }
            }
            // 加载YES
            for (int i = 1; i <= 9; i++)
            {
                string columnName = $"ResultYes{i}";  // 列名
                // 确保列存在
                if (!A3Step8.Columns.Contains(columnName)) continue;

                // 获取 CheckEdit 控件
                Control[] controls = this.Controls.Find($"ckyes{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.CheckEdit checkEdit)
                {
                    // 将 bit (bool) 赋值给 CheckEdit
                    checkEdit.Checked = row[columnName] != DBNull.Value && Convert.ToBoolean(row[columnName]);
                }
            }
            // 加载NO
            for (int i = 1; i <= 9; i++)
            {
                string columnName = $"ResultNo{i}";  // 列名
                // 确保列存在
                if (!A3Step8.Columns.Contains(columnName)) continue;

                // 获取 CheckEdit 控件
                Control[] controls = this.Controls.Find($"ckno{i}", true);
                if (controls.Length > 0 && controls[0] is DevExpress.XtraEditors.CheckEdit checkEdit)
                {
                    // 将 bit (bool) 赋值给 CheckEdit
                    checkEdit.Checked = row[columnName] != DBNull.Value && Convert.ToBoolean(row[columnName]);
                }
            }


            //措施超期文字变成红色
            for (int i = 1; i <= 9; i++)
            {
                TextEdit txtAction = (TextEdit)this.Controls.Find($"txtaction{i}", true).FirstOrDefault();
                CheckEdit ckYes = (CheckEdit)this.Controls.Find($"ckyes{i}", true).FirstOrDefault();
                CheckEdit ckNo = (CheckEdit)this.Controls.Find($"ckno{i}", true).FirstOrDefault();
                DateEdit dtWhen = (DateEdit)this.Controls.Find($"dtwhen{i}", true).FirstOrDefault();

                if (txtAction != null && ckYes != null && ckNo != null && dtWhen != null)
                {
                    if (!string.IsNullOrWhiteSpace(txtAction.Text)) // txtactionX 有内容
                    {
                        if (!(ckYes.Checked || ckNo.Checked)) // ckyesX 和 cknoX 都未选中
                        {
                            if (dtWhen.EditValue != null && dtWhen.DateTime < DateTime.Now) // 时间小于当前时间
                            {
                                dtWhen.ForeColor = Color.Red;
                            }
                            else
                            {
                                dtWhen.ForeColor = Color.Black;
                            }
                        }
                    }
                }
            }


            //获取选中A3Step1的数据(Duedate)
            DataTable A3Step1 = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step1", $"A3ReportNumberID = '{rootcause.A3number}'");
            //超期图片变红
            object dbTimeObj = A3Step1.Rows[0]["Duedate"];//获取数据库中的日期数据
            DateTime dbTime;
            if (dbTimeObj != DBNull.Value && DateTime.TryParse(dbTimeObj.ToString(), out dbTime))
            {
                if (dbTime < DateTime.Now)
                {
                    bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", "Step8 = 3", $"ID = '{rootcause.A3number}' and Step8 != 1");
                    //Load A3list 获得step3的值
                    DataTable A3listTemp = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_List", $"ID = '{rootcause.A3number}'");
                    //获取上方图片状态
                    step1 = (int)A3listTemp.Rows[0][6];
                    step2 = (int)A3listTemp.Rows[0][7];
                    step3 = (int)A3listTemp.Rows[0][8];
                    step4 = (int)A3listTemp.Rows[0][9];
                    step5 = (int)A3listTemp.Rows[0][10];
                    step6 = (int)A3listTemp.Rows[0][11];
                    step7 = (int)A3listTemp.Rows[0][12];
                    step8 = (int)A3listTemp.Rows[0][13];
                    step9 = (int)A3listTemp.Rows[0][14];
                    LoadPicture(step1, step2, step3, step4, step5, step6, step7, step8, step9);
                }
                else
                {
                    return;
                }
            }
        }
        private void InitComboBoxes()
        {
            // 1️⃣ 获取所有需要修改的 ComboBoxEdit 控件
            List<ComboBoxEdit> comboBoxList = new List<ComboBoxEdit>
        {
            cobstatus1, cobstatus2, cobstatus3, cobstatus4, cobstatus5,
            cobstatus6, cobstatus7, cobstatus8, cobstatus9
        };

            // 2️⃣ 创建 ImageList 并添加图片
            ImageList imageList = new ImageList();
            imageList.ImageSize = new Size(16, 16); // 设定图片大小
            imageList.Images.Add("进度0", Properties.Resources.进度0);
            imageList.Images.Add("进度25", Properties.Resources.进度25);
            imageList.Images.Add("进度50", Properties.Resources.进度50);
            imageList.Images.Add("进度75", Properties.Resources.进度75);
            imageList.Images.Add("进度100", Properties.Resources.进度100);

            // 3️⃣ 创建 RepositoryItemImageComboBox（复用）
            RepositoryItemImageComboBox imageComboBox = new RepositoryItemImageComboBox();
            imageComboBox.SmallImages = imageList;

            // 添加选项，绑定图片和值
            imageComboBox.Items.Add(new ImageComboBoxItem("", 1, 0));
            imageComboBox.Items.Add(new ImageComboBoxItem("", 2, 1));
            imageComboBox.Items.Add(new ImageComboBoxItem("", 3, 2));
            imageComboBox.Items.Add(new ImageComboBoxItem("", 4, 3));
            imageComboBox.Items.Add(new ImageComboBoxItem("", 5, 4));

            // 让图片和文本居中
            imageComboBox.GlyphAlignment = DevExpress.Utils.HorzAlignment.Center; // 图片居中
            imageComboBox.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; // 文字居中

            // 4️⃣ 遍历所有 ComboBoxEdit 并应用配置
            foreach (ComboBoxEdit comboBox in comboBoxList)
            {
                comboBox.Properties.Assign(imageComboBox);
            }
        }//为Status添加图片
        private void SetButtonEnable(Control control)//检索所有TextEdit控件是否有变化，如有变化则不显示Next
        {

            if (control is TextEdit)
            {
                control.TextChanged += TextEditTextChange;
            }
            else
            {
                var controlCollection = control.Controls;
                foreach (Control subControl in controlCollection)
                {
                    SetButtonEnable(subControl);
                }
            }


        }
        private void CheckedChanged(object sender, EventArgs e)//检索所有CheckEdit控件是否有变化，如有变化则不显示Next
        {
            for (int i = 1; i <= 9; i++)
            {
                // 找到对应的控件
                var ckyes = this.Controls.Find($"ckyes{i}", true).FirstOrDefault() as CheckEdit;
                var ckno = this.Controls.Find($"ckno{i}", true).FirstOrDefault() as CheckEdit;

                // 绑定事件
                if (ckyes != null) ckyes.CheckedChanged += CheckEdit_CheckedChanged2;
                if (ckno != null) ckno.CheckedChanged += CheckEdit_CheckedChanged2;
            }
        }
        private void CheckEdit_CheckedChanged2(object sender, EventArgs e)
        {
            CheckEdit changedCheck = sender as CheckEdit;

            if (changedCheck != null)
            {
                TextEditTextChange(sender, e);
            }
        }//检索所有CheckEdit控件是否有变化，如有变化则不显示Next
        private void TextEditTextChange(object sender, EventArgs e)
        {

            btnNext.Visible = false;

        }//为SetButtonEnable制作的命令
        private void Step8_Load(object sender, EventArgs e)
        {
            Word();
            LoadA3list();
            LoadA3step8();
            SetButtonEnable(this);//有调整不保存Next键不显示
            CheckedChanged(sender, e);//有CheckEdit调整不保存Next键不显示
            InitComboBoxes();
            palstep1.BackColor = Color.FromArgb(146, 208, 80);
            palstep2.BackColor = Color.FromArgb(146, 208, 80);
            palstep3.BackColor = Color.FromArgb(146, 208, 80);
            palstep4.BackColor = Color.FromArgb(146, 208, 80);
            palstep5.BackColor = Color.FromArgb(146, 208, 80);
            palstep6.BackColor = Color.FromArgb(146, 208, 80);
            palstep7.BackColor = Color.FromArgb(146, 208, 80);
            palstep8.BackColor = Color.FromArgb(146, 208, 80);

            //将默认时间设置为空
            #region
            dtwhen1.Properties.NullDate = DateTime.MinValue; // 允许 NULL
            dtwhen1.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            dtwhen2.Properties.NullDate = DateTime.MinValue; // 允许 NULL
            dtwhen2.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            dtwhen3.Properties.NullDate = DateTime.MinValue; // 允许 NULL
            dtwhen3.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            dtwhen4.Properties.NullDate = DateTime.MinValue; // 允许 NULL
            dtwhen4.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            dtwhen5.Properties.NullDate = DateTime.MinValue; // 允许 NULL
            dtwhen5.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            dtwhen6.Properties.NullDate = DateTime.MinValue; // 允许 NULL
            dtwhen6.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            dtwhen7.Properties.NullDate = DateTime.MinValue; // 允许 NULL
            dtwhen7.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            dtwhen8.Properties.NullDate = DateTime.MinValue; // 允许 NULL
            dtwhen8.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            dtwhen9.Properties.NullDate = DateTime.MinValue; // 允许 NULL
            dtwhen9.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
            #endregion
            CheckEdit_Load(sender, e);//绑定CheckEdit互斥事件
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            bool hasUnjudgedAction = false;
            bool hasAnyYes = false;

            for (int i = 1; i <= 9; i++)
            {
                var txt = this.Controls.Find($"txtaction{i}", true).FirstOrDefault() as TextEdit;
                var yes = this.Controls.Find($"ckyes{i}", true).FirstOrDefault() as CheckEdit;
                var no = this.Controls.Find($"ckno{i}", true).FirstOrDefault() as CheckEdit;

                if (txt != null && yes != null && no != null)
                {
                    if (!string.IsNullOrWhiteSpace(txt.Text))
                    {
                        if (!yes.Checked && !no.Checked)
                        {
                            hasUnjudgedAction = true;
                        }
                    }

                    if (yes.Checked)
                    {
                        hasAnyYes = true;
                    }
                }
            }

            if (hasUnjudgedAction)
            {
                MessageBox.Show("存在未判断的验证措施", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (!hasAnyYes)
            {
                MessageBox.Show("永久措施至少有一个结果为Yes", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (hasAnyYes && !hasUnjudgedAction)
            {
                bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", "Step8 = 1", $"ID = '{rootcause.A3number}'");
                if (reslist == true)
                {
                    Step9 frm = new Step9();
                    frm.Show();
                    this.Hide();
                }

            }
            
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            
            Step7 frm = new Step7();
            frm.Show();
            this.Hide();

        }

        private void lbl4_Click(object sender, EventArgs e)
        {
            pal4.Visible = true;
            lbl4.Visible = false;
            lbl5.Visible = true;
        }

        private void lbl5_Click(object sender, EventArgs e)
        {
            pal5.Visible = true;
            lbl5.Visible = false;
            lbl6.Visible = true;
        }

        private void lbl6_Click(object sender, EventArgs e)
        {
            pal6.Visible = true;
            lbl6.Visible = false;
            lbl7.Visible = true;
        }

        private void lbl7_Click(object sender, EventArgs e)
        {
            pal7.Visible = true;
            lbl7.Visible = false;
            lbl8.Visible = true;
        }

        private void lbl8_Click(object sender, EventArgs e)
        {
            pal8.Visible = true;
            lbl8.Visible = false;
            lbl9.Visible = true;
        }

        private void lbl9_Click(object sender, EventArgs e)
        {
            pal9.Visible = true;
            lbl9.Visible = false;
            
        }
        //CheckEdit互斥
        private void SetCheckPairExclusive(CheckEdit source, CheckEdit target)
        {
            if (source.Checked)
            {
                target.Checked = false;
            }
        }
        private void CheckEdit_Load(object sender, EventArgs e)
        {
            for (int i = 1; i <= 9; i++)
            {
                var yes = this.Controls.Find($"ckyes{i}", true).FirstOrDefault() as CheckEdit;
                var no = this.Controls.Find($"ckno{i}", true).FirstOrDefault() as CheckEdit;

                if (yes != null && no != null)
                {
                    yes.CheckedChanged += (s, e2) => SetCheckPairExclusive(yes, no);
                    no.CheckedChanged += (s, e2) => SetCheckPairExclusive(no, yes);
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DataTable A3Step8 = LSQL.SQL.ReturnDataTable2(SystemInfo.SQL, "A3_Step8", $"A3ReportNumberID = '{rootcause.A3number}'");
            if (A3Step8.Rows.Count > 0)
            {

                string content = $@"Action1 = '{txtaction1.Text}',Action2 = '{txtaction2.Text}',Action3 = '{txtaction3.Text}',Action4 = '{txtaction4.Text}',Action5 = '{txtaction5.Text}',Action6 = '{txtaction6.Text}',Action7 = '{txtaction7.Text}',Action8 = '{txtaction8.Text}',Action9 = '{txtaction9.Text}',
                    Who1 = '{txtwho1.Text}',Who2 = '{txtwho2.Text}',Who3 = '{txtwho3.Text}',Who4 = '{txtwho4.Text}',Who5 = '{txtwho5.Text}',Who6 = '{txtwho6.Text}',Who7 = '{txtwho7.Text}',Who8 = '{txtwho8.Text}',Who9 = '{txtwho9.Text}',
                    When1 = '{dtwhen1.DateTime}',When2 = '{dtwhen2.DateTime}',When3 = '{dtwhen3.DateTime}',When4 = '{dtwhen4.DateTime}',When5 = '{dtwhen5.DateTime}',When6 = '{dtwhen6.DateTime}',When7 = '{dtwhen7.DateTime}',When8 = '{dtwhen8.DateTime}',When9 = '{dtwhen9.DateTime}',
                    Status1 = '{cobstatus1.EditValue}',Status2 = '{cobstatus2.EditValue}',Status3 = '{cobstatus3.EditValue}',Status4 = '{cobstatus4.EditValue}',Status5 = '{cobstatus5.EditValue}',Status6 = '{cobstatus6.EditValue}',Status7 = '{cobstatus7.EditValue}',Status8 = '{cobstatus8.EditValue}',Status9 = '{cobstatus9.EditValue}',
                    ResultYes1 = '{ckyes1.Checked}',ResultYes2 = '{ckyes2.Checked}',ResultYes3 = '{ckyes3.Checked}',ResultYes4 = '{ckyes4.Checked}',ResultYes5 = '{ckyes5.Checked}',ResultYes6 = '{ckyes6.Checked}',ResultYes7 = '{ckyes7.Checked}',ResultYes8 = '{ckyes8.Checked}',ResultYes9 = '{ckyes9.Checked}',
                    ResultNo1 = '{ckno1.Checked}',ResultNo2 = '{ckno2.Checked}',ResultNo3 = '{ckno3.Checked}',ResultNo4 = '{ckno4.Checked}',ResultNo5 = '{ckno5.Checked}',ResultNo6 = '{ckno6.Checked}',ResultNo7 = '{ckno7.Checked}',ResultNo8 = '{ckno8.Checked}',ResultNo9 = '{ckno9.Checked}'";
                bool resstep8 = LSQL.SQL.Update(SystemInfo.SQL, "A3_Step8", content, $"A3ReportNumberID = '{rootcause.A3number}'");
                bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", "Step8 = 2", $"ID = '{rootcause.A3number}'");
                if (resstep8 == true && reslist == true)
                {

                    MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Step8_Load(sender, e);
                    btnNext.Visible = true;
                }
                else
                {
                    MessageBox.Show("更新失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }

            }

            else
            {
                string colunm = "[A3ReportNumberID],[Action1],[Action2],[Action3],[Action4],[Action5],[Action6],[Action7],[Action8],[Action9]," +
                    "[Who1],[Who2],[Who3],[Who4],[Who5],[Who6],[Who7],[Who8],[Who9]," +
                    "[When1],[When2],[When3],[When4],[When5],[When6],[When7],[When8],[When9]," +
                    "[Status1],[Status2],[Status3],[Status4],[Status5],[Status6],[Status7],[Status8],[Status9]," +
                    "[ResultYes1],[ResultYes2],[ResultYes3],[ResultYes4],[ResultYes5],[ResultYes6],[ResultYes7],[ResultYes8],[ResultYes9]," +
                    "[ResultNo1],[ResultNo2],[ResultNo3],[ResultNo4],[ResultNo5],[ResultNo6],[ResultNo7],[ResultNo8],[ResultNo9]";
                string content = $@"'{rootcause.A3number}','{txtaction1.Text}','{txtaction2.Text}','{txtaction3.Text}','{txtaction4.Text}','{txtaction5.Text}','{txtaction6.Text}','{txtaction7.Text}','{txtaction8.Text}','{txtaction9.Text}',
                    '{txtwho1.Text}','{txtwho2.Text}','{txtwho3.Text}','{txtwho4.Text}','{txtwho5.Text}','{txtwho6.Text}','{txtwho7.Text}','{txtwho8.Text}','{txtwho9.Text}',
                    '{dtwhen1.DateTime}','{dtwhen2.DateTime}','{dtwhen3.DateTime}','{dtwhen4.DateTime}','{dtwhen5.DateTime}','{dtwhen6.DateTime}','{dtwhen7.DateTime}','{dtwhen8.DateTime}','{dtwhen9.DateTime}',
                    '{cobstatus1.EditValue}','{cobstatus2.EditValue}','{cobstatus3.EditValue}','{cobstatus4.EditValue}','{cobstatus5.EditValue}','{cobstatus6.EditValue}','{cobstatus7.EditValue}','{cobstatus8.EditValue}','{cobstatus9.EditValue}',
                    '{ckyes1.Checked}','{ckyes2.Checked}','{ckyes3.Checked}','{ckyes4.Checked}','{ckyes5.Checked}','{ckyes6.Checked}','{ckyes7.Checked}','{ckyes8.Checked}','{ckyes9.Checked}',
                    '{ckno1.Checked}','{ckno2.Checked}','{ckno3.Checked}','{ckno4.Checked}','{ckno5.Checked}','{ckno6.Checked}','{ckno7.Checked}','{ckno8.Checked}','{ckno9.Checked}'";
                int res = LSQL.SQL.Insert(SystemInfo.SQL, "A3_Step8", colunm, content);
                if (res == 1)
                {
                    bool reslist = LSQL.SQL.Update(SystemInfo.SQL, "A3_List", "Step8 = 2", $"ID = '{rootcause.A3number}'");
                    if (reslist == true)
                    {
                        MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Step8_Load(sender, e);
                        btnNext.Visible = true;
                    }
                }
                else
                {
                    MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                }

            }
        }

        private void btnClosed_Click(object sender, EventArgs e)
        {
            // 弹出确认对话框，询问是否关闭
            DialogResult result = MessageBox.Show("是否确定关闭？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                // 用户选择“是”，执行关闭操作
                this.Close(); // 关闭当前窗体
            }
            else
            {
                // 用户选择“否”，返回，不做任何操作
                return;
            }
        }
        //点击图片跳转功能
        private void picstep1_Click(object sender, EventArgs e)
        {
            if (step1 == 4)
            {
                return;
            }
            else
            {
                Step1 frm = new Step1();
                this.Close();
                frm.Show();
            }
        }

        private void picstep2_Click(object sender, EventArgs e)
        {
            if (step2 == 4)
            {
                return;
            }
            else
            {
                Step2 frm = new Step2();
                this.Close();
                frm.Show();
            }
        }

        private void picstep3_Click(object sender, EventArgs e)
        {
            if (step3 == 4)
            {
                return;
            }
            else
            {
                Step3 frm = new Step3();
                this.Close();
                frm.Show();
            }
        }
        private void picstep4_Click(object sender, EventArgs e)
        {
            if (step4 == 4)
            {
                return;
            }
            else
            {
                Step4 frm = new Step4();
                this.Close();
                frm.Show();
            }
        }

        private void picstep5_Click(object sender, EventArgs e)
        {
            if (step5 == 4)
            {
                return;
            }
            else
            {
                Step5 frm = new Step5();
                this.Close();
                frm.Show();
            }
        }

        private void picstep6_Click(object sender, EventArgs e)
        {
            if (step6 == 4)
            {
                return;
            }
            else
            {
                Step6 frm = new Step6();
                this.Close();
                frm.Show();
            }
        }

        private void picstep7_Click(object sender, EventArgs e)
        {
            if (step7 == 4)
            {
                return;
            }
            else
            {
                Step7 frm = new Step7();
                this.Close();
                frm.Show();
            }
        }

        private void picstep8_Click(object sender, EventArgs e)
        {
            if (step8 == 4)
            {
                return;
            }
            else
            {
                Step8 frm = new Step8();
                this.Close();
                frm.Show();
            }
        }

        private void picstep9_Click(object sender, EventArgs e)
        {
            if (step9 == 4)
            {
                return;
            }
            else
            {
                Step9 frm = new Step9();
                this.Close();
                frm.Show();
            }
        }
    }
}